package com.molichuxing.gateway.api.services.impl;

import com.molichuxing.framework.exception.BizException;
import com.molichuxing.framework.result.ResponseResult;
import com.molichuxing.gateway.api.services.AdvertisingApiService;
import com.molichuxing.gateway.bff.advertising.response.AdvertisingInfoVo;
import com.molichuxing.gateway.bff.advertising.services.AdvertisingBffService;
import com.molichuxing.services.business.dto.response.StoreBaseBizDto;
import com.molichuxing.services.business.service.StoreBizService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: renzhengyu
 * @Description:
 * @Date: Created in 11:36 2019/10/30
 * @Modified By:
 **/
@RestController
@RequestMapping(value = "/advertising")
@Api(tags = "广告")
public class AdvertisingApiServiceImpl implements AdvertisingApiService {

    private static final Logger logger = LoggerFactory.getLogger(AdvertisingApiServiceImpl.class);

    @Resource
    private AdvertisingBffService advertisingBffService;

    @Resource
    private StoreBizService storeBizService;

    @Override
    @ApiOperation(value = "广告获取")
    @GetMapping(value = "/get")
    public ResponseResult<List<AdvertisingInfoVo>> getAdvertisingInfoVoList(
            @RequestHeader(value = "deviceNum", required = true) String deviceNum) {
        ResponseResult<List<AdvertisingInfoVo>> result = new ResponseResult<>();
        try {
            Assert.hasLength(deviceNum,"cpu不能为空");
            StoreBaseBizDto store= storeBizService.getStoreBaseByCpu(deviceNum);
            if(null == store) {
                throw new BizException(101,"该门店信息不存在");
            }
            return result.success(advertisingBffService.getAdvertisingInfoVoList(store.getStoreId()));
        }catch (Exception e) {
            logger.error("[getAdvertisingInfoVoList]广告获取异常", e);
            result.error("广告获取异常");
        }
        return result;
    }
}
